package commands;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;

import persistence.CConfiger;
import persistence.IPermissiable;
import persistence.IStorage;

public class Commandhome implements ICommand, IPermissiable, IStorage {

	final static String CONST_COMMAND_NAME = "home";
	final static String CONST_CONFIG_NAME = "PlayerHome.yml";
	CCommandHandler handler;
	CConfiger cfgPlayerHome;
	List<Permission> permList = Arrays.asList(new Permission("bcommand.home"));
	public HashMap<UUID, Location> playerHome = new HashMap<UUID, Location>();

	public Commandhome(CCommandHandler h) {
		handler = h;
		cfgPlayerHome = new CConfiger(handler.parent.getParent(),
				CONST_CONFIG_NAME);
	}

	@Override
	public String getCommandName() {
		return CONST_COMMAND_NAME;
	}

	@Override
	public boolean isCommandAccepted(String cmd) {
		return CONST_COMMAND_NAME.equals(cmd) || "h".equals(cmd);
	}

	@Override
	public boolean exec(CommandSender sender, String[] args) {
		if (!checkSender(sender)) {
			sender.sendMessage("Permission Denied");
			return false;
		}

		if (sender instanceof Player) {
			if (playerHome.containsKey(((Player) sender).getUniqueId())) {
				Player p = (Player) sender;
				p.teleport(playerHome.get(p.getUniqueId()));
				return true;
			} else {
				sender.sendMessage("You must set a home to teleport.");
				return false;
			}
		} else {
			if (args.length == 1) {
				Collection<? extends Player> online = handler.parent
						.getParent().getServer().getOnlinePlayers();
				for (Object op : online) {
					Player p = (Player) op;
					if (p.getName().equals(args[0])) {
						if (playerHome.containsKey(p.getUniqueId())) {
							return p.teleport(playerHome.get(p.getUniqueId()));
						} else {
							sender.sendMessage("Home not exist");
							return false;
						}
					}
				}
				for (Object op : online) {
					Player p = (Player) op;
					if (p.getName().matches(args[0])) {
						if (playerHome.containsKey(p.getUniqueId())) {
							return p.teleport(playerHome.get(p.getUniqueId()));
						} else {
							sender.sendMessage("Home not exist");
							return false;
						}
					}
				}
				sender.sendMessage("Player not exist or not set a home.");
				return false;
			} else {
				sender.sendMessage("Wrong argument!");
			}
		}
		return true;
	}

	@Override
	public boolean checkSender(CommandSender sender) {
		for (Permission p : getPermList()) {
			if (!sender.hasPermission(p)) {
				return false;
			}
		}
		return true;
	}
	
	@Override
	public List<Permission> getPermList() {
		return permList;
	}

	@Override
	public boolean LoadData() {
		if (cfgPlayerHome == null)
			cfgPlayerHome = new CConfiger(handler.parent.getParent(),
					CONST_CONFIG_NAME);
		playerHome.clear();
		for (String uuid : cfgPlayerHome.getKeys(false)) {
			World world = handler.parent
					.getParent()
					.getServer()
					.getWorld(
							UUID.fromString(cfgPlayerHome.getString(uuid
									+ ".world")));
			Double x = cfgPlayerHome.getDouble(uuid + ".x");
			Double y = cfgPlayerHome.getDouble(uuid + ".y");
			Double z = cfgPlayerHome.getDouble(uuid + ".z");
			playerHome.put(UUID.fromString(uuid), new Location(world, x, y, z));
			
		}
		return true;
	}

	@Override
	public boolean SaveData() {
		for (UUID uid : playerHome.keySet()) {
			String uuid = uid.toString();
			Location loc = playerHome.get(uid);
			cfgPlayerHome.set(uuid + ".world", loc.getWorld().getUID()
					.toString());
			cfgPlayerHome.set(uuid + ".x", loc.getX());
			cfgPlayerHome.set(uuid + "。y", loc.getY());
			cfgPlayerHome.set(uuid + "。z", loc.getZ());
		}
		return true;
	}

	@Override
	public boolean ReloadData() {
		LoadData();
		return true;
	}

	@Override
	public File getData() {
		return cfgPlayerHome.getSpecifyFile();
	}

	@Override
	public String getDataPath() {
		return cfgPlayerHome.getSpecifyFile().getPath();
	}
	
	public boolean setHome(Player p, Location l)
	{
		playerHome.put(p.getUniqueId(), p.getLocation());
		cfgPlayerHome.set(p.getUniqueId() + "。world", p.getWorld().getName());
		cfgPlayerHome.set(p.getUniqueId() + ".x", p.getLocation().getX());
		cfgPlayerHome.set(p.getUniqueId() + ".y", p.getLocation().getY());
		cfgPlayerHome.set(p.getUniqueId() + ".z", p.getLocation().getZ());
		cfgPlayerHome.saveConfig();
		return true;
	}

}
